#include <QtGui>

#include "fabricTreeItem.h"
#include "src/fabric.h"
#include "fabricDialog.h"

FabricDialog::FabricDialog(FabricTreeItem *_item, QWidget *_parent)
    : QDialog(_parent), item(_item){

    fabric = item->getFabric();
    ho = fabric->getDensity();
    thickness = fabric->getThickness();

    setWindowTitle(QString::fromUtf8("Modificando tecido ").append(QString::number(fabric->getID())));

    createLayout();
}

FabricDialog::~FabricDialog(){
}

void FabricDialog::createLayout(){
    //--------------------------------------ho
    QDoubleSpinBox *hoDoubleSpinBox = new QDoubleSpinBox();
    hoDoubleSpinBox->setRange(0.1, 100000.0);
    hoDoubleSpinBox->setSingleStep(100);
    hoDoubleSpinBox->setValue(ho);
    connect(hoDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeHo(double)));

    QHBoxLayout *hoLayout = new QHBoxLayout;
    hoLayout->addWidget(hoDoubleSpinBox);
    hoLayout->addStretch(1);

    QGroupBox *hoGroupBox = new QGroupBox(QString::fromUtf8("Densidade (ρ)"));
    hoGroupBox->setLayout(hoLayout);

    //--------------------------------------thickness
    QDoubleSpinBox *thicknessDoubleSpinBox = new QDoubleSpinBox();
    thicknessDoubleSpinBox->setRange(0.00001, 10.0);
    thicknessDoubleSpinBox->setSingleStep(0.0001);
    thicknessDoubleSpinBox->setDecimals(5);
    thicknessDoubleSpinBox->setValue(thickness);
    connect(thicknessDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeThickness(double)));

    QHBoxLayout *thicknessLayout = new QHBoxLayout;
    thicknessLayout->addWidget(thicknessDoubleSpinBox);
    thicknessLayout->addStretch(1);

    QGroupBox *thicknessGroupBox = new QGroupBox(QString::fromUtf8("Espessura"));
    thicknessGroupBox->setLayout(thicknessLayout);


    //--------------------------------------Botões
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(acceptChange()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    buttonBox->setLocale(QLocale(QLocale::Portuguese, QLocale::Brazil));


    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(hoGroupBox);
    mainLayout->addWidget(thicknessGroupBox);
    mainLayout->addWidget(buttonBox);
    mainLayout->addStretch(1);

    setLayout(mainLayout);
}


void FabricDialog::acceptChange(){
    fabric->setDensityAndThickness(ho, thickness);

    emit done();

    accept();
}

void FabricDialog::changeHo(double _ho)
{
    ho = _ho;
}

void FabricDialog::changeThickness(double _t)
{
    thickness = _t;
}
